//! 基本节点

use std::mem::transmute;

pub trait Node {
    fn get_literal(&self) -> &str;

    // 获取动态trait的类型的名字
    fn get_type(&self) -> &str;
}

pub fn cast_down_type<T>(node: &dyn Node) -> &T {
    //因为NodeRoot是第一个位置
    unsafe {
        let ptr = transmute::<&dyn Node, (*const (), *const ())>(node);
        let ptr2 = transmute::<*const (), *const T>(ptr.0);
        &*ptr2
    }
}

pub fn cast_down_type_mut<T>(node: &mut dyn Node) -> &mut T {
    unsafe {
        let ptr = transmute::<&dyn Node, (*const (), *const ())>(node);
        let ptr2 = transmute::<*const (), *mut T>(ptr.0);
        &mut *ptr2
    }
}

#[derive(Debug, Clone)]
pub struct NodeRoot {
    token_literal: String,
    r#type: String,
}

impl NodeRoot {
    pub fn new(token_literal: String, r#type: String) -> Self {
        Self {
            token_literal,
            r#type,
        }
    }
}

impl Node for NodeRoot {
    fn get_literal(&self) -> &str {
        &self.token_literal
    }

    fn get_type(&self) -> &str {
        &self.r#type
    }
}
